package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.badibadi.infos.JuLeBan_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Edit_Poly_Activity extends CreateNewPoly1Activity {

	private Results results, results2;
	private JuLeBan_Model ban_Model;
	private String uid;
	private String status = null, img_status = null;
	//这是聚乐板的id ,上一个界面需要传过来的，只有他
	private String photoid;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(Edit_Poly_Activity.this);
				Utils.showMessage(Edit_Poly_Activity.this,getResources().getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(Edit_Poly_Activity.this);
				setData();
				break;
			case 3:
				Utils.ExitPrgress(Edit_Poly_Activity.this);
				Utils.showMessage(Edit_Poly_Activity.this, getResources().getString(R.string.SystemError));
				break;
			case 4:
				Utils.ExitPrgress(Edit_Poly_Activity.this);
				Utils.showMessage(Edit_Poly_Activity.this, getResources().getString(R.string.l_xb55));
				break;
			case 5:
				Utils.ExitPrgress(Edit_Poly_Activity.this);
				Utils.showMessage(Edit_Poly_Activity.this, getResources().getString(R.string.TheOperationFailure));
				break;
				
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// goToTheTitle();
		uid = Utils.getUid(Edit_Poly_Activity.this);
		photoid=getIntent().getStringExtra("photoid");
		show_photo(photoid);//测试用的是"7"
		create_new_poly1_name = (EditText) findViewById(R.id.create_new_poly1_name);
		create_new_poly1_finish = (Button) findViewById(R.id.create_new_poly1_finish);
		create_new_poly1_finish.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String name = create_new_poly1_name.getText().toString().trim();
				if (ban_Model != null && ban_Model.getId() != null
						&& status != null && img_status != null
						&& ban_Model.getIs_mainboard() != null && name != null) {
					edit_Photo(ban_Model.getId(), uid, name, status,
							ban_Model.getIs_mainboard(), img_status);
				} else {

				}
			}
		});
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		super.init();
		create_new_poly1_tital.setText(getResources().getString(R.string.ModifyTheClubPoly));

	}

	private void setData() {
		try {
			create_new_poly1_name.setText(ban_Model.getName());
			setJurisdictionOnClickListener(SetStatus(Integer.valueOf(ban_Model
					.getStatus())));
			setJurisdictionOnClickListener(Setimg_type(Integer
					.valueOf(ban_Model.getImg_type())));
		} catch (Exception e) {
			   
		}

	}

	private int Setimg_type(int s) {
		switch (s) {
		case 0:
			return 10;
		case 1:

			return 11;
		case 2:

			return 12;

		default:
			return 10;
		}
	}

	private int SetStatus(int s) {
		switch (s) {
		case 0:
			return 2;
		case 1:

			return 0;
		case 2:

			return 1;

		default:
			return 0;
		}
	}

	@Override
	public void setJurisdictionOnClickListener(int Click) {
		switch (Click) {
		case 0:
			status = "1";
			i_0.setVisibility(View.VISIBLE);
			i_1.setVisibility(View.GONE);
			i_2.setVisibility(View.GONE);
			break;
		case 1:
			status = "2";
			i_0.setVisibility(View.GONE);
			i_1.setVisibility(View.VISIBLE);
			i_2.setVisibility(View.GONE);
			break;
		case 2:
			status = "0";
			i_0.setVisibility(View.GONE);
			i_1.setVisibility(View.GONE);
			i_2.setVisibility(View.VISIBLE);
			break;

		case 10:
			img_status = "0";
			i_10.setVisibility(View.VISIBLE);
			i_11.setVisibility(View.GONE);
			i_12.setVisibility(View.GONE);
			break;
		case 11:
			img_status = "1";
			i_10.setVisibility(View.GONE);
			i_11.setVisibility(View.VISIBLE);
			i_12.setVisibility(View.GONE);
			break;
		case 12:
			img_status = "2";
			i_10.setVisibility(View.GONE);
			i_11.setVisibility(View.GONE);
			i_12.setVisibility(View.VISIBLE);
			break;
		}
	}
/**
 * 在这里修改
 * @param photoid
 * @param uid
 * @param name
 * @param status
 * @param is_mainboard
 * @param img_type
 */
	private void edit_Photo(final String photoid, final String uid,
			final String name, final String status, final String is_mainboard,
			final String img_type) {
		Utils.showPrgress(Edit_Poly_Activity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				try {
					map.put("photo",
							fengzhuang_xiugaijuleban(photoid, uid, name,
									status, is_mainboard, img_type));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					   
				}
//				   ("传出的参数是:" + map.toString());
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "photo/edit_Photo");
				if (content != null) {
					results2 = Utils.checkResult_NNN(getApplicationContext(),
							content);
//					   ("获取到的修改聚乐版内容是:" + content);
					if (results2 != null
							&& !results2.getRetmsg().equals("null")
						&& results2.isRet()==true) {
						handler.sendEmptyMessage(4);
					} else {
						handler.sendEmptyMessage(5);
					}
				} else {
					handler.sendEmptyMessage(1);
				}

			}
		});
	}

	private JSONObject fengzhuang_xiugaijuleban(final String photoid,
			final String uid, final String name, final String status,
			final String is_mainboard, final String img_type)
			throws JSONException {
		JSONObject object = new JSONObject();
		object.put("photoId", photoid);// 修改的相册ID
		object.put("uid", uid);// 操作者（修改）用户ID
		object.put("name", name);// 修改后的图片名称
		object.put("status", status);// 相册个人空间权限("0":自己可见 "1":所以人可见
										// "2":朋友可见)
		object.put("is_mainboard", is_mainboard);// 是否是主板（"0"：否，"1"：是）
		object.put("img_type", img_type);// 修改的相册所属类型("0":足迹类，"1"：梦想类，"2"：收藏类)
		return object;
	}

	private void show_photo(final String photoid) {
		Utils.showPrgress(Edit_Poly_Activity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("photoId", photoid);
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "photo/show_photo");
				if (content != null) {
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
						try {
//							   ("获取到的聚乐版内容是:" + content);
							ban_Model = new JuLeBan_Model();
							ban_Model = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), JuLeBan_Model.class);
							handler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}

			}
		});
	}
}
